package com.eibus.applicationconnector.twitter.methods.AccountMethods;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import twitter4j.Twitter;
import twitter4j.User;

import com.eibus.applicationconnector.twitter.ApplicationConfiguration;
import com.eibus.applicationconnector.twitter.ISoapRequestContext;
import com.eibus.applicationconnector.twitter.ITwitterMethod;

public class VerifyCredentials implements ITwitterMethod {

	private ApplicationConfiguration configuration;
	private Twitter twitter;
	@Override
	public void cleanup() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean initialize(ApplicationConfiguration acConfig)
			throws Exception {
		this.configuration =acConfig;
		twitter = acConfig.getTwitter();
		return false;
	}

	@Override
	public void onReset() {
		

	}

	@Override
	public EResult process(ISoapRequestContext req) throws Exception {

		int requestNode = req.getRequestRootNode();

        // Get the needed parameters from the SOAP request
       /* String sFileName = XMLUtils.getStringParameter(requestNode, PARAM_FILENAME, true);
        String sLineSeparator = XMLUtils.getStringParameter(requestNode, PARAM_LINESEPARATOR, "");*/
		User user =twitter.verifyCredentials();
		JAXBContext context = JAXBContext.newInstance(User.class);
		Marshaller m = context.createMarshaller();
	    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
	    StringWriter writer = new StringWriter();
	    m.marshal(user, writer);
		req.addResponseElement("response", writer.toString());
		return EResult.FINISHED;
	}

	@Override
	public String getMethodName() {
		return "VerifyCredentials";
	}

}
